package com.mcptt.http.auth.process.result;

import org.json.JSONException;
import org.json.JSONObject;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.http.auth.process.result.info.IdTokenResult;


public class FinalResult extends AuthResult
{
	private String error = "";
	private IdTokenResult idToken = null;
	private String accessToken = "";
	private String refreshToken = "";
	private String tokenType = "";
	private int expires = 0;


	public FinalResult(int result,String desc)
	{
		super(result,desc);
		try
		{
			JSONObject obj = new JSONObject(desc);
			if(result == 200)
			{
				if(!obj.isNull("id_token"))
				{
					String value = obj.getString("id_token");
					this.idToken = new IdTokenResult(value);
				}
				this.accessToken = obj.getString("access_token");
				this.refreshToken = obj.getString("refresh_token");
				this.tokenType = obj.getString("token_type");
				String value = obj.getString("expires_in");
				this.expires = CheckData.parseInt(value);
			}
			else if(result == 400)
			{
				this.error = obj.getString("error");
			}
		} catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean checkReturnParamOk()
	{
		if(idToken == null
				|| !idToken.checkReturnParamOk())
		{
			return false;
		}
		if(accessToken == null
				|| accessToken.length() <= 0)
		{
			return false;
		}
		if(refreshToken == null
				|| refreshToken.length() <= 0)
		{
			return false;
		}
		if(tokenType == null
				|| tokenType.length() <= 0)
		{
			return false;
		}
		if(expires <= 0)
		{
			return false;
		}
		return true;
	}

	public boolean checkRefreshParamOk()
	{
		if(accessToken == null
				|| accessToken.length() <= 0)
		{
			return false;
		}
		if(refreshToken == null
				|| refreshToken.length() <= 0)
		{
			return false;
		}
		if(tokenType == null
				|| tokenType.length() <= 0)
		{
			return false;
		}
		if(expires <= 0)
		{
			return false;
		}
		return true;
	}

	public String getErrStr()
	{
		StringBuffer strBuf = new StringBuffer("FinalResult  ");
		strBuf.append("result=").append(result);
		if(error != null
				&& error.length() >0)
		{
			strBuf.append(",");
			strBuf.append("error=").append(error).append(";");
		}
		else if(desc != null
				&& desc.length() >0)
		{
			strBuf.append(",");
			strBuf.append("desc=").append(desc).append(";");
		}

		return strBuf.toString();
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("FinalResult  ");
		if(result == 200)
		{
			strBuf.append("idToken=").append(idToken).append(",");
			strBuf.append("accessToken=").append(accessToken).append(",");
			strBuf.append("refreshToken=").append(refreshToken).append(",");
			strBuf.append("tokenType=").append(tokenType).append(",");
			strBuf.append("expires=").append(expires).append(";");
		}
		else if(result == 400)
		{
			strBuf.append("error=").append(error).append(";");
		}
		else
		{
			return super.toString();
		}

		return strBuf.toString();
	}

	public boolean isSuc()
	{
		if(result == 200)
		{
			return true;
		}
		return false;
	}

	public String getError()
	{
		return error;
	}

	public IdTokenResult getIdToken()
	{
		return idToken;
	}
	public void setIdToken(IdTokenResult idToken)
	{
		this.idToken = idToken;
	}


	public String getAccessToken()
	{
		return accessToken;
	}


	public String getRefreshToken()
	{
		return refreshToken;
	}


	public String getTokenType()
	{
		return tokenType;
	}

	public int getExpires()
	{
		return expires;
	}
}
